$(document).ready(function () {
    /*查询科目*/
    $.ajax({
        type: "POST",
        url: "fields",
        data: {},
        dataType: "json",
        success: function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#field").html($("#field").html() + '<option value="' + data[i].fieldId + '">' + data[i].fieldName + '</option>');
            }
        }
    });

    /*查询试题类型*/
    Qtype();
});

function Qtype() {
    $.ajax({
        type: "POST",
        url: "questionTypes",
        data: {},
        dataType: "json",
        success: function (data) {
            for (var i = 4; i < data.length; i++) {
                $("select[name='typeId']").html($("select[name='typeId']").html() + '<option value="' + data[i].questionTypeId + '")>' + data[i].questionTypeName + '</option>');
            }
        }
    });
}

function isnull() {
//    alert(document.getElementsByTagName("input").length)
    var inputObj = document.getElementsByTagName("input");
//    console.log(inputObj);
    //只能输入数字
    var reg = /^[0-9]\d*$/;
    for (var i = 0; i < inputObj.length; i++) {
        if (inputObj[i].type == "text") {
            if (!reg.test(inputObj[i].value)) {
                inputObj[i].focus();
                inputObj[i].style.background = "red";
                layer.alert("试题数量只能输入整数数字！");
                inputObj[i].style.background = "white";
                return false;
                break;
            }
        }
    }
    //	alert("成功：数据都不为空！");
    return true;
}

function fzwl() {
    var inputObj2 = document.getElementsByTagName("input");
    for (var i = 0; i < inputObj2.length; i++) {
        if (inputObj2[i].type == "text") {
            if ((inputObj2[i].value == "") || (inputObj2[i].value == null)) {
                //    alert(inputObj2[i].id);
                var objid = document.getElementById(inputObj2[i].id);
                $(objid).val("0");
                continue;
            }
        }
    }
}

//点击下拉框时当前的试题类型
var typechose;
//下拉框点击事件
function testclick() {
 //   alert("bbbb");
    typechose = $("#typeId").val();
}

//所有主观题的内容
var zgtValue = "";
var zgtcount = 0;
//主观题下拉框改变事件
function selectChange() {
 //   alert("aaaa");
    var a = $("#jdcount4").val();
    var b = $("#zdcount4").val();
    var c = $("#ntcount4").val();
    if(a == ""){
        a = 0;
    }
    else {
        a = parseInt(a);
    }
    if(b == ""){
        b = 0;
    }
    else {
        b = parseInt(b);
    }
    if(c == ""){
        c = 0;
    }
    else {
        c = parseInt(c);
    }
 //   alert("a="+a+"b="+b+"c="+c);
    //主观题类型选择
    var zgtxz =typechose;
    var zgjd = a;
    var zgzd = b;
    var zgn = c;
    zgtcount += zgjd + zgzd + zgn;
 //   alert("zgjd:" + a + "zgzd:" + b + "zgn:" + c);
 //   alert("zgtcount:"+zgtcount);
    zgtValue += zgtxz + "," + zgjd + "," + zgzd + "," + zgn + "&";
//    alert("zgtValue:" + zgtValue + "chosetype:" + zgtxz);
    $("#jdcount4").val("");
    $("#zdcount4").val("");
    $("#ntcount4").val("");
}

function commit() {
    //获取当前name为zgt的input数量
//    var els = document.getElementsByName("zgt").length;
//    alert(els.length/3);
    //如果输入框为空，则赋值为0
    fzwl();
    zgtValue += $("#typeId").val() + "," + parseInt($("#jdcount4").val()) + "," + parseInt($("#zdcount4").val()) + "," + parseInt($("#ntcount4").val());
    zgtcount += parseInt($("#jdcount4").val()) + parseInt($("#zdcount4").val()) + parseInt($("#ntcount4").val());
//    alert("zgtValue:"+zgtValue);
//    alert("zgtcount:"+zgtcount);
    //科目选择
    var fieldxz = $("#field").val();
    var xzjd = parseInt($("#jdcount1").val());
    var xzzd = parseInt($("#zdcount1").val());
    var xzn = parseInt($("#ntcount1").val());
    var pdjd = parseInt($("#jdcount2").val());
    var pdzd = parseInt($("#zdcount2").val());
    var pdn = parseInt($("#ntcount2").val());
    var tkjd = parseInt($("#jdcount3").val());
    var tkzd = parseInt($("#zdcount3").val());
    var tkn = parseInt($("#ntcount3").val());
    var grade = xzjd*2 + xzzd*2 + xzn*2 + tkjd*2 + tkzd*2 + tkn*2 + pdjd*2 + pdzd*2 + pdn*2 + zgtcount*10;
//    alert(grade);
    if (isnull() && grade == 100) {
 //   if (grade == 100) {
        $.ajax({
            type: "POST",
            url: "addStrategies",
            data: {
                "fieldxz":fieldxz,"xzjd":xzjd,"xzzd":xzzd,"xzn":xzn,"pdjd":pdjd,"pdzd":pdzd,
                "pdn":pdn,"tkjd":tkjd,"tkzd":tkzd,"tkn":tkn,"zgtValue":zgtValue
            },
            dataType: "json",
            success: function (data) {
                if(data == 1){
                    layer.alert("提交成功");
                }
                else {
                    layer.alert("其他原因，提交失败1");
                }
            },
            error: function () {
                layer.alert("其他原因，提交失败2");
            }
        });
    }
    else if (grade > 100) {
        zgtcount = 0;
        zgtValue = "";
        layer.alert("题目分数为：" + grade + "分，超出了100分，请重新组卷");
    }
    else if (grade < 100) {
        zgtcount = 0;
        zgtValue = "";
        layer.alert("题目分数为：" + grade + "分，不足100分，请重新组卷");
    }
}